{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "60f2cf9b-b305-4cea-a48f-83be750ebc19",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-16T01:09:56.809354Z",
     "iopub.status.busy": "2022-02-16T01:09:56.809061Z",
     "iopub.status.idle": "2022-02-16T01:09:57.075050Z",
     "shell.execute_reply": "2022-02-16T01:09:57.074475Z",
     "shell.execute_reply.started": "2022-02-16T01:09:56.809284Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc02de8a-d51a-4969-9bec-02dcf8415fe4",
   "metadata": {},
   "source": [
    "1. 通过ndarray.T的形式进行转置\n",
    "2. 通过ndarray.transpose的形式进行转置\n",
    "    * 该方法返回的是原数组的View，修改新数组的值会影响原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ebba5327-bbe0-4cf5-83d4-a3202e430cd0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-16T01:18:57.024808Z",
     "iopub.status.busy": "2022-02-16T01:18:57.024538Z",
     "iopub.status.idle": "2022-02-16T01:18:57.032921Z",
     "shell.execute_reply": "2022-02-16T01:18:57.032183Z",
     "shell.execute_reply.started": "2022-02-16T01:18:57.024783Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t1:\n",
      "[[5 3 3 8 1]\n",
      " [3 5 9 3 3]\n",
      " [2 2 9 2 1]]\n",
      "====================\n",
      "t1.T:\n",
      "[[5 3 2]\n",
      " [3 5 2]\n",
      " [3 9 9]\n",
      " [8 3 2]\n",
      " [1 3 1]]\n",
      "====================\n",
      "t1*t1.T:\n",
      "[[108  84  60]\n",
      " [ 84 133 106]\n",
      " [ 60 106  94]]\n"
     ]
    }
   ],
   "source": [
    "# 使用ndarray的T属性进行转置\n",
    "t1 = np.random.randint(0, 10, size=(3, 5))  # 创建3行5列的数组t1，其中元素为0到9之间的随机整数\n",
    "print(\"t1:\\n{}\".format(t1))\n",
    "print(\"=\" * 20)\n",
    "t1t=t1.T # 获取数组t1的转置\n",
    "print(\"t1.T:\\n{}\".format(t1t)) \n",
    "print(\"=\" * 20)\n",
    "print(\"t1*t1.T:\\n{}\".format(t1.dot(t1t))) # 计算t1与其转置的点乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "32b77ed8-a361-4595-a8bb-64c37c2544ea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-16T01:34:16.859457Z",
     "iopub.status.busy": "2022-02-16T01:34:16.859211Z",
     "iopub.status.idle": "2022-02-16T01:34:16.865921Z",
     "shell.execute_reply": "2022-02-16T01:34:16.865286Z",
     "shell.execute_reply.started": "2022-02-16T01:34:16.859430Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t3:\n",
      "[[3 0 5 4 0]\n",
      " [7 7 6 6 5]\n",
      " [2 6 8 6 0]]\n",
      "====================\n",
      "t3.transpose():\n",
      "[[3 7 2]\n",
      " [0 7 6]\n",
      " [5 6 8]\n",
      " [4 6 6]\n",
      " [0 5 0]]\n",
      "====================\n",
      "t3.transpose():\n",
      "[[100 100 100]\n",
      " [  0   7   6]\n",
      " [  5   6   8]\n",
      " [  4   6   6]\n",
      " [  0   5   0]]\n",
      "t3:\n",
      "[[100   0   5   4   0]\n",
      " [100   7   6   6   5]\n",
      " [100   6   8   6   0]]\n"
     ]
    }
   ],
   "source": [
    "# 使用ndarray的transpose函数进行转置\n",
    "t3 = np.random.randint(0, 10, size=(3, 5))  # 创建3行5列的数组t3，其中元素为0到9之间的随机整数\n",
    "print(\"t3:\\n{}\".format(t3))\n",
    "print(\"=\" * 20)\n",
    "t3t=t3.transpose() # 获取数组t3的转置\n",
    "print(\"t3.transpose():\\n{}\".format(t3t)) \n",
    "print(\"=\" * 20)\n",
    "t3t[0]=100 # 修改t3转置数组的第0个行元素值\n",
    "print(\"t3.transpose():\\n{}\".format(t3t)) \n",
    "print(\"t3:\\n{}\".format(t3)) # 对应的t3中第0列元素值也改变了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9741d81-ae45-4414-a83c-2b3611ba403c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
